// import { Login } from "@/api/interface/index";
import { PORT1 } from "@/api/config/servicePort";
// import authMenuList from "@/assets/json/authMenuList.json";
import authButtonList from "@/assets/json/authButtonList.json";
import http from "@/api";
import { userInfoType } from "./type";
import { isPC } from "@/utils/sys";
import { useAppointmentStore } from "@/stores/modules/appointment";

let dbc = [
  {
    path: "/mobileIndex/index",
    name: "mobileIndex",
    component: "/mobileIndex/index",
    meta: {
      icon: "icon-laboratory",
      title: "首页",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: false,
      isKeepAlive: true
    }
  },
  {
    path: "/laboratory/index",
    name: "laboratory",
    component: isPC() ? "/laboratory/index" : "/laboratoryMobile/index",
    meta: {
      icon: "icon-laboratory",
      title: "实验室管理",
      isLink: "",
      isCustomIcon: true,
      isHide: false,
      isFull: isPC() ? false : true,

      isAffix: false,
      isKeepAlive: true
    }
  },

  {
    path: "/experimentalist/index",
    name: "experimentalist",
    component: isPC() ? "/experimentalist/index" : "/experimentalistMobile/index",
    meta: {
      icon: "icon-experimentalist",
      title: "实验员管理",
      isLink: "",
      isCustomIcon: true,
      isHide: false,
      isFull: isPC() ? false : true,
      isAffix: false,
      isKeepAlive: true
    }
  },
  {
    path: "/laboratoryAppointment/index",
    name: "laboratoryAppointment",
    // component: isPC() ? "/laboratoryAppointment/index" : "/laboratoryMobile/index",
    meta: {
      icon: "icon-laboratoryAppointment",
      title: "预约管理",
      isLink: "",
      isCustomIcon: true,
      isHide: false,
      isFull: isPC() ? false : true,
      isAffix: false,
      isKeepAlive: true
    },
    children: [
      // 预约审核
      {
        path: "/appointmentAudit/index",
        name: "appointmentAudit",
        component: isPC() ? "/appointmentAudit/index" : "/appointmentAudit/index",
        meta: {
          icon: "",
          title: "预约审核",
          isLink: "",
          isCustomIcon: true,
          isHide: false,
          isFull: isPC() ? false : true,
          isAffix: false,
          isKeepAlive: true
        }
      },
      // 借出审核
      {
        path: "/lendReview/index",
        name: "lendReview",
        component: isPC() ? "/lendReview/index" : "/lendReview/index",
        meta: {
          icon: "",
          title: "借出审核",
          isLink: "",
          isCustomIcon: true,
          isHide: false,
          isFull: isPC() ? false : true,
          isAffix: false,
          isKeepAlive: true
        }
      },
      // 实验室列表
      {
        path: "/laboratoryList/index",
        name: "laboratoryList",
        component: isPC() ? "/laboratoryList/index" : "/laboratoryList/index",
        meta: {
          icon: "icon-laboratoryList",
          title: "实验室列表",
          isLink: "",
          isCustomIcon: true,
          isHide: false,
          isFull: isPC() ? false : true,
          isAffix: false,
          isKeepAlive: true
        }
      },
      // 仪器列表
      {
        path: "/instrumentList/index",
        name: "instrumentList",
        component: isPC() ? "/instrumentList/index" : "/instrumentList/index",
        meta: {
          icon: "icon-instrumentList",
          title: "仪器列表",
          isLink: "",
          isCustomIcon: true,
          isHide: false,
          isFull: isPC() ? false : true,
          isAffix: false,
          isKeepAlive: true
        }
      }
    ]
  },
  {
    path: "/goods/index",
    name: "goods",
    // component: "/goods/index",
    component: isPC() ? "/goods/index" : "/goodsMobile/index",
    meta: {
      icon: "icon-goods",
      title: "货品管理",
      isLink: "",
      isCustomIcon: true,
      isHide: false,
      isFull: isPC() ? false : true,
      isAffix: true,
      isKeepAlive: true
    }
  },

  {
    path: "/addGoodsMobile/index",
    name: "addGoodsMobile",
    // component: "/goods/index",
    component: "/addGoodsMobile/index",
    meta: {
      icon: "icon-goods",
      title: "添加货品",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  },
  {
    path: "/exportGoodsExcelMobile/index",
    name: "exportGoodsExcelMobile",
    // component: "/goods/index",
    component: "/exportGoodsExcelMobile/index",
    meta: {
      icon: "icon-goods",
      title: "导出货品",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  },
  {
    path: "/lendGoodsMobile/index",
    name: "lendGoodsMobile",
    // component: "/goods/index",
    component: "/lendGoodsMobile/index",
    meta: {
      icon: "icon-goods",
      title: "借出货品",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  },
  {
    path: "/returnGoodsMobile/index",
    name: "returnGoodsMobile",
    // component: "/goods/index",
    component: "/returnGoodsMobile/index",
    meta: {
      icon: "icon-goods",
      title: "归还货品",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  },
  {
    path: "/goodsMobile/index",
    name: "goodsMobile",
    component: "/goodsMobile/index",
    meta: {
      icon: "icon-goods",
      title: "货品管理",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  },
  {
    path: "/category/index",
    name: "category",
    component: isPC() ? "/category/index" : "/categoryMobile/index",
    meta: {
      icon: "icon-category",
      title: "类别管理",
      isLink: "",
      isCustomIcon: true,
      isHide: false,
      isFull: isPC() ? false : true,
      isAffix: false,
      isKeepAlive: true
    }
  },
  {
    path: "/measurement/index",
    name: "measurement",
    component: isPC() ? "/measurement/index" : "/measurementMobile/index",
    meta: {
      icon: "icon-metrics",
      title: "度量管理",
      isLink: "",
      isCustomIcon: true,
      isHide: false,
      isFull: isPC() ? false : true,
      isAffix: false,
      isKeepAlive: true
    }
  },
  {
    path: "/selectGood/index",
    name: "selectGood",
    // component: "/goods/index",
    component: "/selectGood/index",
    meta: {
      icon: "icon-goods",
      title: "选择货品",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  },
  {
    path: "/lendReturnLogMobile/index",
    name: "lendReturnLogMobile",
    // component: "/goods/index",
    component: "/lendReturnLogMobile/index",
    meta: {
      icon: "icon-goods",
      title: "借还记录",
      isLink: "",
      isCustomIcon: true,
      isHide: true,
      isFull: true,
      isAffix: true,
      isKeepAlive: true
    }
  }
];

// 权限Name列表
const roleNameList = [
  {
    isM: 0,
    names: [
      "goods",
      "goodsMobile",
      "mobileIndex",
      "exportGoodsExcelMobile",
      "addGoodsMobile",
      "lendReturnLogMobile",
      "selectGood",
      "returnGoodsMobile",
      "lendGoodsMobile",
      "laboratoryAppointment"
    ]
  },
  {
    isM: 1,
    names: [
      "goods",
      "laboratory",
      "mobileIndex",
      "experimentalist",
      "category",
      "measurement",
      "goodsMobile",
      "exportGoodsExcelMobile",
      "addGoodsMobile",
      "lendReturnLogMobile",
      "selectGood",
      "returnGoodsMobile",
      "laboratoryAppointment"
    ]
  }
];

// 实验室预约页面权限用户id列表
// const laboratoryAppointmentPageUserIds = [32, 135, 145];

/**
 * @name 登录模块
 */
// 用户登录
// export const loginApi = (params: Login.ReqLoginForm) => {
//   return http.post<Login.ResLogin>(PORT1 + `/login`, params, { loading: false }); // 正常 post json 请求  ==>  application/json
// return http.post<Login.ResLogin>(PORT1 + `/login`, params, { loading: false }); // 控制当前请求不显示 loading
// return http.post<Login.ResLogin>(PORT1 + `/login`, {}, { params }); // post 请求携带 query 参数  ==>  ?username=admin&password=123456
// return http.post<Login.ResLogin>(PORT1 + `/login`, qs.stringify(params)); // post 请求携带表单参数  ==>  application/x-www-form-urlencoded
// return http.get<Login.ResLogin>(PORT1 + `/login?${qs.stringify(params, { arrayFormat: "repeat" })}`); // get 请求可以携带数组等复杂参数
// };

export const loginApi = (data: { account: string; psw: string }) => {
  // console.log("loginApi", data);
  return http.post<userInfoType>("/tbLabUser/login" + `?account=${data.account}&psw=${data.psw}`);
};

// 是否加入实验室预约判断
const isToLabAppointPageD = (isM, isU) => {
  return isM == 1 || isU == 1 ? true : false;
};

// 获取菜单列表
export const getAuthMenuListApi = () => {
  const appointmentStore = useAppointmentStore();

  // return http.get<Menu.MenuOptions[]>(PORT1 + `/menu/list`, {}, { loading: false });
  // 如果想让菜单变为本地数据，注释上一行代码，并引入本地 authMenuList.json 数据
  let rMenuList = [];
  let userInfo = localStorage.getItem("geeker-user");
  console.log(userInfo);
  let isM = userInfo ? JSON.parse(userInfo as string).userInfo.isManager : 0;
  console.log("此用户的用户信息", userInfo);

  console.log("此用户的isM", isM);
  console.log(dbc);

  // 用户id
  const userId = JSON.parse(userInfo).userInfo.id;

  // 用户信息
  const permissionData = JSON.parse(localStorage.getItem("permission"));

  // 是否有权限进入实验室预约页面
  const isToLaboratoryAppointmentPage = isToLabAppointPageD(permissionData.isManager, permissionData.isUser);

  // 预约实验室身份 0 预约人   1 审核人
  let appointIdentity = 1;

  if (permissionData.isManager == 1) {
    appointIdentity = 1;
  } else if (permissionData.isUser == 1) {
    appointIdentity = 0;
  }

  appointmentStore.judgeIdentity(userId);

  console.log("是否有权限进入实验室预约页面", isToLaboratoryAppointmentPage);

  let roleNames = roleNameList.filter(item => {
    return isM == item.isM;
  })[0].names;
  console.log("权限名称列表", roleNames);
  dbc.forEach(item => {
    if (item.name == "laboratoryAppointment") {
      console.log("pppccccc", item.name);
      if (roleNames.indexOf(item.name) != -1 && isToLaboratoryAppointmentPage) {
        console.log("cccc", item.name);
        console.log("#过滤菜单", appointIdentity);
        if (appointIdentity == 0) {
          // item.children = item.children?.filter(item => {
          //   return item.name == "appointmentAudit";
          // });
          item.children = [
            // 预约审核
            {
              path: "/appointmentAudit/index",
              name: "appointmentAudit",
              component: isPC() ? "/appointmentAudit/index" : "/appointmentAudit/index",
              meta: {
                icon: "",
                title: "预约实验室",
                isLink: "",
                isCustomIcon: true,
                isHide: false,
                isFull: isPC() ? false : true,
                isAffix: false,
                isKeepAlive: true
              }
            },
            // 借出审核
            {
              path: "/lendReview/index",
              name: "lendReview",
              component: isPC() ? "/lendReview/index" : "/lendReview/index",
              meta: {
                icon: "",
                title: "仪器借出",
                isLink: "",
                isCustomIcon: true,
                isHide: false,
                isFull: isPC() ? false : true,
                isAffix: false,
                isKeepAlive: true
              }
            }
          ];
        } else if (appointIdentity == 1) {
          item.children = [
            // 预约审核
            {
              path: "/appointmentAudit/index",
              name: "appointmentAudit",
              component: isPC() ? "/appointmentAudit/index" : "/appointmentAudit/index",
              meta: {
                icon: "",
                title: "预约审核",
                isLink: "",
                isCustomIcon: true,
                isHide: false,
                isFull: isPC() ? false : true,
                isAffix: false,
                isKeepAlive: true
              }
            },
            // 借出审核
            {
              path: "/lendReview/index",
              name: "lendReview",
              component: isPC() ? "/lendReview/index" : "/lendReview/index",
              meta: {
                icon: "",
                title: "借出审核",
                isLink: "",
                isCustomIcon: true,
                isHide: false,
                isFull: isPC() ? false : true,
                isAffix: false,
                isKeepAlive: true
              }
            }
            // 仪器列表
            // {
            //   path: "/laboratoryList/index",
            //   name: "laboratoryList",
            //   component: isPC() ? "/laboratoryList/index" : "/laboratoryList/index",
            //   meta: {
            //     icon: "icon-laboratoryList",
            //     title: "实验室列表",
            //     isLink: "",
            //     isCustomIcon: true,
            //     isHide: false,
            //     isFull: isPC() ? false : true,
            //     isAffix: false,
            //     isKeepAlive: true
            //   }
            // },
            // 仪器列表
            // {
            //   path: "/instrumentList/index",
            //   name: "instrumentList",
            //   component: isPC() ? "/instrumentList/index" : "/instrumentList/index",
            //   meta: {
            //     icon: "icon-instrumentList",
            //     title: "仪器列表",
            //     isLink: "",
            //     isCustomIcon: true,
            //     isHide: false,
            //     isFull: isPC() ? false : true,
            //     isAffix: false,
            //     isKeepAlive: true
            //   }
            // }
          ];
        }
        rMenuList.push(item as never);
      }
    } else {
      if (roleNames.indexOf(item.name) != -1) {
        console.log("cccc", item.name);
        rMenuList.push(item as never);
      }
    }
  });

  // {
  //   path: "/laboratoryAppointment/index",
  //   name: "laboratoryAppointment",
  //   component: isPC() ? "/laboratoryAppointment/index" : "/laboratoryMobile/index",
  //   meta: {
  //     icon: "icon-laboratoryAppointment",
  //     title: "实验室预约",
  //     isLink: "",
  //     isCustomIcon: true,
  //     isHide: false,
  //     isFull: isPC() ? false : true,
  //     isAffix: false,
  //     isKeepAlive: true
  //   }
  // },

  console.log("rMenuList", rMenuList);

  // debugger;

  console.log("此用户的菜单列表", rMenuList);

  let rMenuListData = {
    code: 200,
    data: rMenuList
  };

  return rMenuListData;
};

// 获取按钮权限
export const getAuthButtonListApi = () => {
  // return http.get<Login.ResAuthButtons>(PORT1 + `/auth/buttons`, {}, { loading: false });
  // 如果想让按钮权限变为本地数据，注释上一行代码，并引入本地 authButtonList.json 数据
  return authButtonList;
};

// 用户退出登录
export const logoutApi = () => {
  return http.post(PORT1 + `/logout`);
};
